
var list=[];
var index=0;
var ks=0;
var xh;//循环钩子
var deviceId;
var authorizationCode;

function generateUUID() {
    var d = new Date().getTime();
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = (d + Math.random()*16)%16 | 0;
        d = Math.floor(d/16);
        return (c=='x' ? r : (r&0x3|0x8)).toString(16);
    });
    return uuid;
};


function callback(str){
    eval('(' + str + ')');//动态调用js
}

function shouquan(){
    var code = prompt("请输入授权码", "");
    if(code){
        $.getJSON("https://wx.jnyoubo.com/data/cxsk/shouquan.json",function(json){
            console.log('json',json)
            if(json.code==0){
                var ok=false;
                for(var i=0;i<json['list'].length;i++){
                    if(json['list'][i]==deviceId){
                        ok=true;
                        authorizationCode=json['list'];
                        localStorage.setItem('authorizationCode',"success");
                        console.log('授权成功');
                        location.reload();
                        break;
                    }
                }
                if(!ok){
                    alert('授权失败');
                }
            }
        });
    } else {
        alert("授权码不能为空")
    }
}


function playNext(){

    if(index<list.length){

        if(!authorizationCode){
            if(index>=2){
                alert("设备未授权,请尽快授权");
                return;
            }
        }

        jQuery(list[index]['a']).trigger("click");
        callback(list[index]['href']);
    }
    index++;
    if(index>list.length){
        clearInterval(xh);
        alert('所有课程播放完毕');
    }

}

function play(){

    var className=jQuery('iframe').contents().find(".ans-attach-ct").attr('class');
    if(className.indexOf('finished')>=0){//播放完成了
        playNext();
    }else{
        var but2=jQuery('iframe').contents().find('iframe').contents().find(".vjs-play-control");
        if(jQuery(but2).attr('title')=='播放'){
            //jQuery(but2).trigger("click");
            var video=jQuery('iframe').contents().find('iframe').contents().find("#video_html5_api");
            if (video[0].paused){
                video[0].muted=true;
                video[0].play();
            }
        }
        //console.log('视频播放监控中'+Math.random());
        return;
    }


}


function showTip(){
    var div='<div id="plug" style="font-size:18px;margin-left:200px">';
    var smks='共扫描到'+ks+"节课,其中有"+list.length+"节课未完成";
    var tip='<span style="color:red;font-size:18px" >自动播放插件工作中 '+smks+'</span>';
    div+=tip;

    if(!authorizationCode){
        var sq='<br><span style="color:red;font-size:14px" >设备未授权:设备编号:'+deviceId+' </span>';
        sq+='<br><span style="color:red;font-size:14px" >未授权设备只能自动播放3节课 授权设备请联系QQ:420534221 </span>';
        sq+='<br><a style="color:green;font-size:14px" href="javascript:shouquan();">点我授权</a>';
        div+=sq;

    }

    div+='</div>';
    jQuery("body").before(div);
}

jQuery(function (){




    setTimeout(function(){

        jQuery(".cells").each(function(){
            jQuery(this).find('.ncells').each(function (){
                ks++;
                var clas=jQuery(this).find('.roundpointStudent').attr('class');
                if(clas.indexOf('jobCount')>=0){
                    var title=jQuery(this).find('a').attr('title');//标题
                    var a=jQuery(this).find('a');
                    var href=jQuery(this).find('a').attr('href').replace('javascript:','').replace(';','');//href
                    list.push({'title':title,'a':a,'dev':jQuery(this),'href':href,'play':false});
                }

            });
        });



        xh=setInterval(function (){
            play();
        },1000);




        deviceId=localStorage.getItem('deviceId');
        authorizationCode=localStorage.getItem('authorizationCode');
        if(!deviceId){
            deviceId=generateUUID();
            localStorage.setItem('deviceId',deviceId);
        }
        showTip();//显示授权

        console.log(deviceId)
        console.log('本插件技术含量低,只针对小白,如遇高手请手下留情')

    },100);

});